﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Volo.Abp;
using Volo.Abp.AspNetCore;
using Volo.Abp.AspNetCore.Components.Web.Theming.Routing;
using Volo.Abp.AspNetCore.Mvc.Libs;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.FeatureManagement;
using Volo.Abp.FeatureManagement.Blazor.Server;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.Blazor.Server;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.PermissionManagement;
using Volo.Abp.PermissionManagement.Blazor.Server;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace Tool
{
    [DependsOn(
    // 核心 ABP 模块
    typeof(AbpAspNetCoreModule),
    typeof(AbpEntityFrameworkCoreSqliteModule),

    // Identity 模块
    typeof(AbpIdentityDomainModule),
    typeof(AbpIdentityApplicationModule),
    typeof(AbpIdentityEntityFrameworkCoreModule),
    typeof(AbpIdentityHttpApiModule),
    typeof(AbpIdentityBlazorServerModule),

    // 租户管理
    typeof(AbpTenantManagementDomainModule),
    typeof(AbpTenantManagementApplicationModule),
    typeof(AbpTenantManagementEntityFrameworkCoreModule),
    typeof(AbpTenantManagementHttpApiModule),

    // 权限管理
    typeof(AbpPermissionManagementDomainModule),
    typeof(AbpPermissionManagementApplicationModule),
    typeof(AbpPermissionManagementEntityFrameworkCoreModule),
    typeof(AbpPermissionManagementBlazorServerModule),

    // 设置管理
    typeof(AbpSettingManagementDomainModule),
    typeof(AbpSettingManagementEntityFrameworkCoreModule),

    // 特性管理
    typeof(AbpFeatureManagementDomainModule),
    typeof(AbpFeatureManagementApplicationModule),
    typeof(AbpFeatureManagementEntityFrameworkCoreModule),
    typeof(AbpFeatureManagementHttpApiModule),
    typeof(AbpFeatureManagementBlazorServerModule))
        ]
    public class ToolModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {

            //context.Services.AddAbpDbContext<ToolDbContext>(options =>
            //{
            //    options.AddDefaultRepositories(includeAllEntities: true);
            //});

            Configure<AbpDbContextOptions>(options =>
            {
                options.UseSqlite();
            });
            // 配置连接字符串
            Configure<AbpDbConnectionOptions>(options =>
            {
                options.ConnectionStrings.Default = "Data Source=tool.db";
            });
            Configure<AbpMvcLibsOptions>(options =>
            {
                options.CheckLibs = false;
            });


            Configure<AbpMultiTenancyOptions>(options =>
            {
                options.IsEnabled = true;
            });


            // 禁用所有静态数据保存
            Configure<SettingManagementOptions>(options =>
            {
                options.SaveStaticSettingsToDatabase = false;
                options.IsDynamicSettingStoreEnabled = false;
            });

            Configure<PermissionManagementOptions>(options =>
            {
                options.SaveStaticPermissionsToDatabase = false;
                options.IsDynamicPermissionStoreEnabled = false;
            });

            Configure<FeatureManagementOptions>(options =>
            {
                options.SaveStaticFeaturesToDatabase = false;
                options.IsDynamicFeatureStoreEnabled = false;
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            //var env = context.GetEnvironment();

            //app.UseCorrelationId();
            //app.UseAbpSecurityHeaders();
            //app.UseRouting();
            //app.MapAbpStaticAssets();

            //app.UseAuthentication();


            app.UseUnitOfWork();
            //app.UseDynamicClaims();
            //app.UseAntiforgery();
            //app.UseAuthorization();


            //app.UseAuditing();
        }
    }
}
